package com.zp.business.impl.register;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.register.RegisterPayType;
import com.zp.business.entity.register.bo.RegisterPayTypeBo;
import com.zp.business.entity.register.vo.RegisterPayTypeVo;
import com.zp.business.mapper.register.RegistPayTypeMapper;
import com.zp.business.service.register.IRegistPayTypeService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 挂号收费类型表Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-12-26
 */
@RequiredArgsConstructor
@Service
public class RegistPayTypeServiceImpl implements IRegistPayTypeService {

    private final RegistPayTypeMapper baseMapper;

    /**
     * 查询挂号收费类型表
     */
    @Override
    public RegisterPayTypeVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询挂号收费类型表列表(分页)
     */
    @Override
    public TableDataInfo<RegisterPayTypeVo> queryPageList (RegisterPayTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RegisterPayType> lqw = buildQueryWrapper(bo);
        Page<RegisterPayTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询挂号收费类型表列表(不分页)
     */
    @Override
    public List<RegisterPayTypeVo> queryList (RegisterPayTypeBo bo) {
        LambdaQueryWrapper<RegisterPayType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RegisterPayType> buildQueryWrapper (RegisterPayTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RegisterPayType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), RegisterPayType::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getAcctNo()), RegisterPayType::getAcctNo, bo.getAcctNo());
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), RegisterPayType::getTypeName, bo.getTypeName());
        return lqw;
    }

    /**
     * 新增挂号收费类型表
     */
    @Override
    public Boolean insertByBo (RegisterPayTypeBo bo) {
        RegisterPayType add = BeanUtil.toBean(bo, RegisterPayType.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改挂号收费类型表
     */
    @Override
    public Boolean updateByBo (RegisterPayTypeBo bo) {
        RegisterPayType update = BeanUtil.toBean(bo, RegisterPayType.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (RegisterPayType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除挂号收费类型表
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量保存
     * @param list
     * @return
     */
    @Override
    public Boolean insertBatch (List<RegisterPayType> list) {
        return baseMapper.insertBatch(list);
    }
}
